package com.yj.springbootstudy.mulThread.imooc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 定义一个全局共享变量 count，初始值为0
 * 两个线程同时操作 count 每次+1 做100次
 */
public class Demo01 {

    static int count = 0;
    static Lock lock = new ReentrantLock();


    public static void main(String[] args) {

        for (int i = 0; i < 2 ; i++) {
            new Thread(()->{

                try {
                    lock.lock();
                    for (int j = 0; j < 100 ; j++) {
                        count++;
                    }

                }finally {
                    lock.unlock();
                }


            },"i").start();
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("count value："+count);
    }
}
